/**
 * @file imgviewer.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"
#include "lvgl/src/lv_draw/lv_draw_img_jpeg_decoder.h"

#if USE_LV_WATCH_IMGVIEWER != 0

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/
typedef lv_fs_file_t ui_file_t;

extern lv_obj_t * voice_msg_create_send(lv_obj_t * activity_obj);
/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * imgviewer_photo_bg_create(lv_obj_t * par);
static lv_obj_t * imgviewer_setting_create(lv_obj_t * activity_obj);
static void imgviewer_photo_item_create(lv_obj_t * par);
//static void imgviewer_setting_order_create(lv_obj_t * par);
static void imgviewer_setting_delete_create(lv_obj_t * par, uint8_t del_one);
static void imgviewer_setting_event_cb(lv_obj_t * obj, lv_event_t e);
static void imgviewer_back_event_cb(lv_obj_t * obj, lv_event_t e);
static lv_res_t imgviewer_img_signal(lv_obj_t * obj, lv_signal_t sign, void * param);
static void imgviewer_share_event_cb(lv_obj_t * obj, lv_event_t e);
static void imgviewer_delete_event_cb(lv_obj_t * obj, lv_event_t e);
//static lv_res_t imgviewer_setting_order_signal(lv_obj_t * obj, lv_signal_t sign, void * param);
static void imgviewer_setting_opt_event_cb(lv_obj_t * obj, lv_event_t e);
static lv_res_t imgviewer_item_page_scrl_signal(lv_obj_t * page_scrl, lv_signal_t sign, void * param);
static void imgviewer_prev_event_cb(lv_obj_t * obj, lv_event_t e);
static void imgviewer_next_event_cb(lv_obj_t * obj, lv_event_t e);
static void imgviewer_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void imgviewer_ok_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static imgviewer_photo_info_t * imgviewer_get_del_img_node(lv_obj_t * img, uint8_t type);
static void imgviewer_set_photo_order_label_text(void);
static void imgviewer_view_photo_tabview_create(uint32_t img_index);
static void imgviewer_view_photo_opt_create(lv_obj_t * img_view);
static void imgviewer_prepare_destory(lv_obj_t * activity_obj);
static void imgviewer_reset_photo_ll_selected(void);
static void imgviewer_set_photo_ll_selected(uint32_t num, uint8_t sel);
static void imgviewer_photo_share_confirm_create(lv_obj_t * par, voice_msg_contact_t * contact, void * src);
static void imgviewer_share_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void imgviewer_share_ok_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void imgviewer_update_photo_file(char * path);
static void imgviewer_free_prewview_src(void);
static void imgviewer_reset_node_index(void);
static void imgviewer_recreate_preview(bool reset_preview_mode);
#if USE_LV_BLUETOOTH != 0
static void imgviewer_share_to_voice_msg_action(lv_obj_t * btn, lv_event_t e);
static void imgviewer_share_to_bt_action(lv_obj_t * btn, lv_event_t e);
static void imgviewer_share_select_dev_action(lv_obj_t * btn, lv_event_t e);
#endif
static void imgviewer_get_img_info(char * path, ui_file_t * pfile);
#if IMGVIEWER_WRITE_PATH_FILE
static img_info_t * imgviewer_get_img_info_from_filesystem(uint32_t index, bool mode);
static void imgviewer_store_img_info_to_global(char * path, char * filename, ui_file_t * pfile);
#endif
#if USE_LV_SD_FS
static void imgviewer_sd_mounted_as_storage(lv_obj_t * obj);
#endif
static int imgviewer_check_img_size(char * fullname, uint32_t * img_w, uint32_t * img_h);

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_ll_t * photo_ll = NULL;
static lv_signal_cb_t orignal_page_scrl_signal;
static lv_signal_cb_t orignal_img_signal;
static uint32_t img_file_cnt = 0;
img_info_t img_info_global;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * imgviewer_create(lv_obj_t * activity_obj)
{
    lv_obj_t * bg;
    lv_obj_t * obj;

    printf("%s\n", __FUNCTION__);

    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_IMGVIEWER;
        activity_ext.create = imgviewer_create;
        activity_ext.prepare_destory =  imgviewer_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    /*Allocate the object type specific extended data*/
    lv_imgviewer_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_imgviewer_obj_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    ext->ext_create = false;

#if USE_LV_SD_FS
    if(IMGVIEWER_SD_MOUNTED_AS_U_STORAGE == HAL_sd_user_get()) {
        imgviewer_sd_mounted_as_storage(obj);
        return obj;
    }
#endif
    ext->time_order = 1;
    ext->photo_cnt = imgviewer_photo_ll_get_nodecnt();
    ext->photo_curr_page_start_index = 0;
    ext->photo_tabview_start_index = 0;
    ext->photo_tabview_curr_index = 0;
    ext->preview_mode = 0;
    ext->obj_par = obj;
    ext->selected_cnt = 0;
    ext->from_camera = 0;
    ext->pressing = false;
    ext->del_bg = NULL;
    ext->share_bg = NULL;
    ext->timer = NULL;
    ext->ext_create = true;

    bg = imgviewer_photo_bg_create(obj);

    if(ext->photo_cnt > 0) {
        //imgviewer_reset_node_index();
        imgviewer_photo_item_create(bg);
    }

    return obj;
}

lv_imgviewer_obj_ext_t * imgviewer_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_IMGVIEWER);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_imgviewer_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

void imgviewer_create_event_cb(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;

    if(LV_EVENT_CLICKED == e) {
        /*change gui task to lower priority to keep audio playing smoothly*/
#ifndef BUILD_IN_PC_SIMULATOR
        bool prio_changed = false;
        uint8_t old_prio;

        if(HAL_sd_user_get() > 0) {
            uos_chgpriority_task(gui_task_id, 250, &old_prio);
            prio_changed = true;
        }
#endif

#if USE_LV_SD_FS
#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
        if(false == setting_sd_mount_capture_check(SETTING_CAPTURE_SD_APP_IMGVIEWER))
#else
        if(!(IMGVIEWER_SD_MOUNTED_AS_U_STORAGE == HAL_sd_user_set(true)))
#endif
#endif
        {
            //lv_jpeg_decoder_init();
            lv_watch_png_cache_all_free();
            imgviewer_photo_ll_init();

#if IMGVIEWER_WRITE_PATH_FILE
            hal_rtc_t rtc;

            Hal_Rtc_Gettime(&rtc);
            printf("%s: rtc time1 = %d:%d:%d\n", __FUNCTION__, rtc.tm_hour, rtc.tm_min, rtc.tm_sec);
            imgviewer_get_img_info_from_filesystem(0, true);

            Hal_Rtc_Gettime(&rtc);
            printf("%s: rtc time2 = %d:%d:%d\n", __FUNCTION__, rtc.tm_hour, rtc.tm_min, rtc.tm_sec);

            imgviewer_load_all_photo_path();
            Hal_Rtc_Gettime(&rtc);
            printf("%s: rtc time3 = %d:%d:%d\n", __FUNCTION__, rtc.tm_hour, rtc.tm_min, rtc.tm_sec);
#else
            img_file_cnt = 0;
            imgviewer_get_img_info(IMGVIEWER_ROOT_PATH, NULL);
#endif
        }

        lv_obj_t * obj = imgviewer_create(NULL);
        LV_ASSERT_MEM(obj);

        /*restore gui task priority */
#ifndef BUILD_IN_PC_SIMULATOR
        if(prio_changed) {
            uos_chgpriority_task(gui_task_id, old_prio, &old_prio);
        }
#endif
    }
}

lv_res_t imgviewer_create_from_camera(void)
{
    lv_obj_t * obj = imgviewer_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return LV_RES_OK;

    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    ext->from_camera = 1;
    ext->timer = NULL;
    if(ext->photo_cnt > 0)
        imgviewer_view_photo_tabview_create(0);
    return LV_RES_OK;
}

void imgviewer_photo_ll_init(void)
{
    if(photo_ll == NULL) {
        photo_ll = lv_mem_alloc(sizeof(lv_ll_t));
        memset(photo_ll, 0, sizeof(lv_ll_t));
        _lv_ll_init(photo_ll, sizeof(imgviewer_photo_info_t));
    }
    //imgviewer_photo_path_init();
}

const lv_ll_t * imgviewer_get_photo_ll(void)
{
    return photo_ll;
}

void imgviewer_photo_ll_destroy(void)
{
    if(photo_ll) {
        _lv_ll_clear(photo_ll);
        lv_mem_free(photo_ll);
        photo_ll = NULL;
    }
}

uint32_t imgviewer_photo_ll_get_nodecnt(void)
{
    uint32_t cnt = 0;
    lv_ll_node_t * node = NULL;
    node = _lv_ll_get_head(photo_ll);
    while(node) {
        cnt++;
        node = _lv_ll_get_next(photo_ll, node);
    }
    printf("%s: img cnt = %d\n", __FUNCTION__, cnt);
    return cnt;
}

#if USE_LV_BLUETOOTH != 0
void imgviewer_select_share_way(lv_obj_t * par)
{
    lv_obj_t * bg = lv_cont_create(par, NULL);
    lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));

    lv_obj_t * img_back = lv_img_create(bg, NULL);
    lv_img_set_src(img_back, ICON_IMGVIEWER_BACK_BLUE);
    lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_click(img_back, true);
    lv_watch_set_free_num(img_back, IMGVIEWER_BACK_SEL_SHARE);
    lv_obj_set_event_cb(img_back, imgviewer_back_event_cb);

    lv_obj_t * label = lv_label_create(bg, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SHARE_TO);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0,
                 lv_obj_get_y(img_back) + (lv_obj_get_height(img_back) - lv_obj_get_height(label)) / 2);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLUE);

    lv_obj_t * btn_label;
    lv_obj_t * vm_btn = lv_btn_create(bg, NULL);
    lv_obj_set_size(vm_btn, LV_HOR_RES, LV_VER_RES / 8);
    lv_obj_align(vm_btn, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 15);
    lv_theme_apply(vm_btn, LV_THEME_LIST_BTN);
    lv_obj_set_style_local_bg_opa(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_radius(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_border_width(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 1);
    lv_obj_set_style_local_border_side(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_BORDER_SIDE_BOTTOM);
    lv_obj_set_style_local_border_width(vm_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 1);
    lv_obj_set_style_local_border_side(vm_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_BORDER_SIDE_BOTTOM);
    lv_obj_set_event_cb(vm_btn, imgviewer_share_to_voice_msg_action);
    lv_btn_set_layout(vm_btn, LV_LAYOUT_OFF);
    btn_label = lv_label_create(vm_btn, NULL);
    lv_obj_add_style(btn_label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_label_set_text_id(btn_label, WATCH_TEXT_ID_WECHAT);
    lv_obj_set_pos(btn_label, 10, 5);
    lv_obj_t * bt_btn = lv_btn_create(bg, vm_btn);
    lv_obj_align(bt_btn, vm_btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_event_cb(bt_btn, imgviewer_share_to_bt_action);
    btn_label = lv_label_create(bt_btn, btn_label);
    lv_label_set_text_id(btn_label, WATCH_TEXT_ID_BLUETOOTH);

    lv_obj_t * imgviewer_act_obj = lv_watch_get_activity_obj(ACT_ID_IMGVIEWER);
    lv_obj_t * camera_act_obj = lv_watch_get_activity_obj(ACT_ID_CAMERA);
    if(imgviewer_act_obj == NULL && camera_act_obj) {
        lv_obj_t * watch_obj = NULL;
        lv_camera_obj_ext_t  * ext = NULL;
        lv_watch_get_child_obj(camera_act_obj, lv_watch_obj_signal, &watch_obj);
        if(watch_obj)
            ext = lv_obj_get_ext_attr(watch_obj);
        lv_img_dsc_t * src = (lv_img_dsc_t *)lv_img_get_src(ext->img);
        lv_fs_file_t file;
        lv_fs_res_t res = lv_fs_open(&file, IMGVIEWER_SHARE_TMEP_PATH, LV_FS_MODE_WR);
        if(res == LV_FS_RES_OK) {
            uint32_t rn;
            res = lv_fs_write(&file, (void *)src->data, src->data_size, &rn);
            if(res != LV_FS_RES_OK) {
                printf("write temp bt share file error,errno = %d\n", res);
            } else {
                printf("write temp bt share file OK rn = %d\n", rn);
                lv_watch_set_free_ptr(bt_btn, (void *)IMGVIEWER_SHARE_TMEP_PATH);
            }
            lv_fs_close(&file);
        }
    } else if(imgviewer_act_obj) {
        lv_obj_set_parent(bg, imgviewer_act_obj);
        lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
        ext->share_bg = bg;
        imgviewer_photo_info_t * node = NULL;
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->selected == 1) {
                lv_watch_set_free_ptr(bt_btn, (void *)node);
                break;
            }
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
    }
}

void imgviewer_send_file_finish_action(void)
{
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    if(NULL == ext)
        return;
    if(ext->share_bg) {
        lv_obj_del(ext->share_bg);
        ext->share_bg = NULL;
        imgviewer_recreate_preview(true);
        imgviewer_reset_photo_ll_selected();
    }
}

#endif

void imgviewer_photo_share_event_cb(lv_obj_t * img, lv_event_t e)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_VOICE_MSG);
    if(NULL == activity_obj) return;
    lv_obj_t * obj = NULL;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &obj);
    lv_voice_msg_ext_t  * ext = lv_obj_get_ext_attr(obj);

    if(LV_EVENT_PRESSED == e) {
        ext->pressing = true;
    } else if((true == ext->pressing) && (LV_EVENT_CLICKED == e)) {
        ext->pressing = false;
        /*click the contact*/
        lv_obj_t * img_portrait = lv_obj_get_child_back(img, NULL);
        imgviewer_photo_share_confirm_create(obj, (voice_msg_contact_t *)lv_obj_get_user_data(img_portrait).user_data, (void *)lv_img_get_src(img_portrait));
    }
}

void imgviewer_photo_share_flag_reset(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_IMGVIEWER);
    if(activity_obj) {
        imgviewer_photo_info_t * node = NULL;
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->selected == 1) {
                node->selected = 0;
                break;
            }
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
    }
}

void imgviewer_photo_path_init(void)
{
    lv_fs_file_t path_file;
    lv_fs_res_t res;
    res = lv_fs_open(&path_file, IMGVIEWER_PATH_FILE, LV_FS_MODE_RD);
    if(res == LV_FS_RES_OK) {
        lv_fs_close(&path_file);
    } else {
        res = lv_fs_open(&path_file, IMGVIEWER_PATH_FILE, LV_FS_MODE_WR);
        if(res == LV_FS_RES_OK) {
            lv_fs_close(&path_file);
        } else {
            printf("create file failed\n");
        }
    }
}

void imgviewer_load_one_photo_path(char * path, char * filename, uint32_t img_w, uint32_t img_h)
{
    imgviewer_photo_info_t * node;
    const char * extname = NULL;
    char * fullname = NULL;
    uint32_t len = 0;

    if(img_file_cnt >= IMGVIEWER_MAX_COUNT) {
        return;
    }

    node = (imgviewer_photo_info_t *)_lv_ll_ins_tail(photo_ll);
    node->selected = 0;
    node->img_w = img_w;
    node->img_h = img_h;
    node->index = img_file_cnt++;

    //fullname
    len = (strlen(path) - 1) + strlen(filename) + 1;
    fullname = lv_mem_alloc(len);
    memset(fullname, 0, len);
    strncpy(fullname, path, (strlen(path) - 1)); //without *
    strcat(fullname, filename);
    //printf("%d - %s\n", node->index, fullname);

    //preview src
    node->preview_src = lv_mem_alloc(sizeof(lv_img_dsc_t));
    memset(node->preview_src, 0, sizeof(lv_img_dsc_t));
    node->preview_src->data = (const uint8_t *)fullname;
    node->preview_src->data_size = 0;
    extname = lv_fs_get_ext(fullname);
    if(0 == strcmp(extname, "png")) {
        node->preview_src->header.cf = LV_IMG_CF_RAW_ALPHA;
    } else if(0 == strcmp(extname, "jpg")) {
        node->preview_src->header.cf = LV_IMG_CF_RAW;
    }
    node->preview_src->header.always_zero = 0;
    node->preview_src->header.reserved = 1;
    node->preview_src->header.w = IMGVIEWER_THUMBNAIL_W;
    node->preview_src->header.h = IMGVIEWER_THUMBNAIL_H;
}

void imgviewer_load_all_photo_path(void)
{
    lv_fs_file_t file;
    char * temp_buf = NULL;
    char * path = NULL;
    char * filename = NULL;
    char * fullname = NULL;
    const char * extname = NULL;
    uint32_t len = 0;

    printf("%s\n", __FUNCTION__);

    lv_fs_res_t res = lv_fs_open(&file, IMGVIEWER_PATH_FILE, LV_FS_MODE_RD);
    if(res == LV_FS_RES_OK) {
        uint32_t rn;
        uint32_t size;
        lv_fs_size(&file, &size);
        temp_buf = lv_mem_alloc(size + 1);
        memset(temp_buf, 0, size + 1);
        lv_fs_read(&file, temp_buf, size, &rn);
        lv_fs_close(&file);

        uint32_t i = 0;
        imgviewer_photo_info_t * node;

        for(i = 0; i < img_file_cnt; i++) {
            node = (imgviewer_photo_info_t *)_lv_ll_ins_tail(photo_ll);
            node->selected = 0;
            node->index = i;

            //fullname
            path = &temp_buf[i * IMGVIEWER_PATH_MAX * 2 + sizeof(uint32_t)];
            filename = &temp_buf[i * IMGVIEWER_PATH_MAX * 2 + IMGVIEWER_PATH_MAX + sizeof(uint32_t)];
            len = strlen(path) + strlen(filename) + 1;
            fullname = lv_mem_alloc(len);
            memset(fullname, 0, len);
            strncpy(fullname, path, strlen(path));
            strcat(fullname, filename);

            //preview src
            node->preview_src = lv_mem_alloc(sizeof(lv_img_dsc_t));
            memset(node->preview_src, 0, sizeof(lv_img_dsc_t));
            node->preview_src->data = (const uint8_t *)fullname;
            node->preview_src->data_size = 0;
            extname = lv_fs_get_ext(fullname);
            if(0 == strcmp(extname, "png")) {
                node->preview_src->header.cf = LV_IMG_CF_RAW_ALPHA;
            } else if(0 == strcmp(extname, "jpg")) {
                node->preview_src->header.cf = LV_IMG_CF_RAW;
            }
            node->preview_src->header.always_zero = 0;
            node->preview_src->header.reserved = 1;
            node->preview_src->header.w = IMGVIEWER_THUMBNAIL_W;
            node->preview_src->header.h = IMGVIEWER_THUMBNAIL_H;

            //printf("%d - %s\n", node->index, fullname);
        }
        lv_mem_free(temp_buf);
    } else {
        printf("%s: open photo_path file failed\n", __FUNCTION__);
    }
}

lv_fs_res_t imgviewer_photo_path_write(char * str)
{
    lv_fs_file_t path_file;
    lv_fs_res_t res = lv_fs_open(&path_file, IMGVIEWER_PATH_FILE, (LV_FS_MODE_RD | LV_FS_MODE_APPEND));
    if(res == LV_FS_RES_OK) {
        uint32_t rn;
        uint32_t size;
        uint32_t cnt = imgviewer_photo_ll_get_nodecnt();
        lv_fs_size(&path_file, &size);
        if(cnt > 1) {
            if((cnt - 1) * IMGVIEWER_PATH_LEN > size) {
                printf("seek path file error: cnt:%d, file size:%d\n", cnt, size);
                lv_fs_close(&path_file);
                return LV_FS_RES_UNKNOWN;
            }
            lv_fs_seek(&path_file, (cnt - 1) * IMGVIEWER_PATH_LEN);
        }
        res = lv_fs_write(&path_file, (void *) str, IMGVIEWER_PATH_LEN + 1, &rn);
        if(res != LV_FS_RES_OK)
            printf("write path file error res = %d\n", res);
        lv_fs_close(&path_file);
    } else {
        printf("open path file error res = %d\n", res);
    }

    return res;
}

void imgviewer_destory(bool start_cam)
{
    printf("%s\n", __FUNCTION__);

    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    if(ext->ext_create) {
        if(ext->timer) {
            Hal_Timer_Stop(ext->timer);
            ext->timer = NULL;
        }
        imgviewer_free_prewview_src();
        if(ext->from_camera == 0 || start_cam == false) {
            imgviewer_photo_ll_destroy();
        } else {
            camera_start_from_album();
        }
    }
#if USE_LV_SD_FS
#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
    setting_sd_mount_app_release(SETTING_CAPTURE_SD_APP_IMGVIEWER);
#else
    if(HAL_sd_user_get()) {   
        HAL_sd_user_set(false);
    }
#endif
#endif
    lv_watch_png_cache_all_free();
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void imgviewer_prepare_destory(lv_obj_t * activity_obj)
{
    imgviewer_destory(true);
}

static void imgviewer_setting_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    if(ext->del_bg) {
        uint32_t img_num = lv_obj_get_user_data(lv_obj_get_child_back(ext->del_bg, NULL)).user_num;
        imgviewer_photo_info_t * node = NULL;
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->index == img_num) {
                node->preview_src->header.w = IMGVIEWER_THUMBNAIL_W;
                node->preview_src->header.h = IMGVIEWER_THUMBNAIL_H;
                break;
            }
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
        lv_obj_del(ext->del_bg);
        ext->del_bg = NULL;
    }
}

static void imgviewer_free_prewview_src(void)
{
    imgviewer_photo_info_t * node = NULL;
    node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
    while(node) {
        if(node->preview_src) {
            lv_img_cache_invalidate_src(node->preview_src);
            lv_mem_free(node->preview_src->data);
            lv_mem_free(node->preview_src);
            node->preview_src = NULL;
        }
        node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
    }
}

static void imgviewer_update_photo_file(char * path)
{
}

static void imgviewer_reset_node_index(void)
{
    uint32_t i = 0;
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    imgviewer_photo_info_t * node = NULL;
    if(ext->time_order)
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
    else
        node = (imgviewer_photo_info_t *)_lv_ll_get_tail(photo_ll);
    while(node) {
        node->index = i;
        if(ext->time_order)
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        else
            node = (imgviewer_photo_info_t *)_lv_ll_get_prev(photo_ll, node);
        i++;
    }
}

static imgviewer_photo_info_t * imgviewer_get_del_img_node(lv_obj_t * img, uint8_t type)
{
    uint32_t i = 0;
    imgviewer_photo_info_t * node = NULL;
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    lv_ll_t * list = (lv_ll_t *)imgviewer_get_photo_ll();
    if(type == 1) {//del one
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(list);
        for(i = 0; i < ext->photo_cnt; i++) {
            if(node->index ==  lv_obj_get_user_data(img).user_num) {
                node->selected = 1;
                return node ;
            }
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(list, node);
        }
    } else if(type == 3) { // del all
        if(ext->time_order)
            node = (imgviewer_photo_info_t *)_lv_ll_get_head(list);
        else
            node = (imgviewer_photo_info_t *)_lv_ll_get_tail(list);

        return node;
    } else if(type == 2) {//del muti
        if(ext->time_order)
            node = (imgviewer_photo_info_t *)_lv_ll_get_tail(list);
        else
            node = (imgviewer_photo_info_t *)_lv_ll_get_head(list);

        for(i = 0; i < ext->photo_cnt; i++) {
            if(node->selected == 1) {
                return node;
            }
            if(ext->time_order) {
                node = (imgviewer_photo_info_t *)_lv_ll_get_prev(list, node);
            } else {
                node = (imgviewer_photo_info_t *)_lv_ll_get_next(list, node);
            }
        }
    }
    return NULL;
}

static void imgviewer_photo_delete(uint8_t type)
{
    imgviewer_photo_info_t * node = NULL;
    imgviewer_photo_info_t * next_node = NULL;
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    lv_ll_t * list = (lv_ll_t *)imgviewer_get_photo_ll();
    node = (imgviewer_photo_info_t *)_lv_ll_get_head(list);
    while(node) {
        //next node
        next_node = (imgviewer_photo_info_t *)_lv_ll_get_next(list, node);
        if(((type == 1 || type == 2) && node->selected == 1) || type == 3) {
            lv_fs_remove((const char *)node->preview_src->data);
            //printf("%s: %s, index=%d\n", __FUNCTION__, node->preview_src->data, node->index);
            imgviewer_update_photo_file((char *)node->preview_src->data);
            lv_img_cache_invalidate_src(node->preview_src);
            lv_mem_free(node->preview_src->data);
            lv_mem_free(node->preview_src);
            _lv_ll_remove(list, node);
            lv_mem_free(node);
            if(type == 1)
                break;
        }
        node = next_node;
    }
    ext->photo_cnt = imgviewer_photo_ll_get_nodecnt();
    imgviewer_reset_node_index();

    if(ext->photo_cnt <= IMGVIEWER_IMG_COUNT_PER_PAGE) {
        ext->photo_curr_page_start_index = 0;
    } else {
        while(ext->photo_curr_page_start_index >= ext->photo_cnt) {
            ext->photo_curr_page_start_index -= IMGVIEWER_IMG_COUNT_PER_PAGE;
        }
    }
    printf("%s: photo_curr_page_start_index=%d\n", __FUNCTION__, ext->photo_curr_page_start_index);
}

static void imgviewer_set_photo_ll_selected(uint32_t num, uint8_t sel)
{
    uint32_t i = 0;
    imgviewer_photo_info_t * node = NULL;
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    lv_ll_t * list = (lv_ll_t *)imgviewer_get_photo_ll();
    if(ext->time_order)
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(list);
    else
        node = (imgviewer_photo_info_t *)_lv_ll_get_tail(list);
    for(i = 0; i < ext->photo_cnt; i++) {
        if(i == num)
            node->selected = sel;
        if(ext->time_order)
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(list, node);
        else
            node = (imgviewer_photo_info_t *)_lv_ll_get_prev(list, node);
    }
}

static void imgviewer_reset_photo_ll_selected(void)
{
    uint32_t i = 0;
    imgviewer_photo_info_t * node = NULL;
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    lv_ll_t * list = (lv_ll_t *)imgviewer_get_photo_ll();

    node = (imgviewer_photo_info_t *)_lv_ll_get_head(list);
    for(i = 0; i < ext->photo_cnt; i++) {
        node->selected = 0;
        node = (imgviewer_photo_info_t *)_lv_ll_get_next(list, node);
    }
    ext->selected_cnt = 0;
}

static lv_obj_t * imgviewer_get_obj(void)
{
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_IMGVIEWER);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    return watch_obj;
}
static void imgviewer_set_photo_order_label_text()
{
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    if(ext == NULL || ext->orderopt_label == NULL)
        return;

    char opt[25];
    memset(opt, 0, 25);
    if(ext->time_order) {
        snprintf(opt, 25, "%s", (char *)lv_lang_get_text(WATCH_TEXT_ID_TIME_ORDER));
    } else {
        snprintf(opt, 25, "%s", (char *)lv_lang_get_text(WATCH_TEXT_ID_TIME_REVERSE_ORDER));
    }
    lv_label_set_text(ext->orderopt_label, opt);
}

static lv_obj_t * imgviewer_photo_bg_create(lv_obj_t * par)
{
    lv_obj_t * bg;
    lv_obj_t * img_setting = NULL;
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    if(ext->photo_cnt == 0) {
        bg = lv_img_create(par, NULL);
        lv_img_set_src(bg, ICON_IMGVIEWER_NO_PHOTO);

        lv_obj_t * label_nophoto = lv_label_create(bg, NULL);
        lv_label_set_text_id(label_nophoto, WATCH_TEXT_ID_NOPHOTO);
        lv_obj_align(label_nophoto, NULL, LV_ALIGN_IN_BOTTOM_MID, 10, -20);
        lv_obj_add_style(label_nophoto, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
        lv_obj_set_click(label_nophoto, false);

    } else {
        bg = lv_cont_create(par, NULL);
        lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
        lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));

        img_setting = lv_img_create(bg, NULL);
        lv_img_set_src(img_setting, ICON_IMGVIEWER_MORE_SETTING);
        lv_obj_set_click(img_setting, true);
        lv_obj_set_adv_hittest(img_setting, false);
        lv_obj_align(img_setting, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
    }
    lv_obj_set_click(bg, false);

    //back
    lv_obj_t * img_back = lv_img_create(bg, NULL);
    lv_img_set_src(img_back, ICON_IMGVIEWER_BACK_BLUE);
    lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_watch_set_free_num(img_back, IMGVIEWER_BACK_QUIT);
    lv_obj_set_click(img_back, true);
    lv_obj_set_event_cb(img_back, imgviewer_back_event_cb);
    lv_obj_set_adv_hittest(img_back, false);

    if(ext->photo_cnt > 0) {
        //prev
        lv_obj_t * cont_prev = lv_cont_create(bg, NULL);
        lv_obj_set_size(cont_prev, lv_obj_get_width(img_back), lv_obj_get_height(img_back));
        lv_obj_set_style_local_bg_color(cont_prev, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));
        lv_obj_align(cont_prev, img_back, LV_ALIGN_OUT_RIGHT_TOP, 0, 0);
        lv_obj_set_click(cont_prev, true);

        lv_obj_t * label_prev = lv_label_create(cont_prev, NULL);
        lv_label_set_text(label_prev, LV_SYMBOL_PREV);
        lv_obj_add_style(label_prev, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
        lv_obj_set_style_local_text_font(label_prev, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
        lv_obj_align(label_prev, NULL, LV_ALIGN_CENTER, 0, 0);

        //next
        lv_obj_t * cont_next = lv_cont_create(bg, cont_prev);
        lv_obj_set_size(cont_next, lv_obj_get_width(img_back), lv_obj_get_height(img_back));
        lv_obj_align(cont_next, img_setting, LV_ALIGN_OUT_LEFT_TOP, 0, 0);
        lv_obj_set_click(cont_next, true);

        lv_obj_t * label_next = lv_label_create(cont_next, label_prev);
        lv_label_set_text(label_next, LV_SYMBOL_NEXT);
        lv_obj_align(label_next, NULL, LV_ALIGN_CENTER, 0, 0);

        if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(bg)) {
            lv_obj_set_event_cb(cont_prev, imgviewer_next_event_cb);
            lv_obj_set_event_cb(cont_next, imgviewer_prev_event_cb);
        } else {
            lv_obj_set_event_cb(cont_prev, imgviewer_prev_event_cb);
            lv_obj_set_event_cb(cont_next, imgviewer_next_event_cb);
        }

        //page
        lv_obj_t * label_page = lv_label_create(bg, NULL);
        lv_obj_add_style(label_page, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
        char page[8];
        uint32_t total_page = ext->photo_cnt % IMGVIEWER_IMG_COUNT_PER_PAGE == 0
                              ? ext->photo_cnt / IMGVIEWER_IMG_COUNT_PER_PAGE : ext->photo_cnt / IMGVIEWER_IMG_COUNT_PER_PAGE + 1;
        uint32_t curr_page = ext->photo_curr_page_start_index / IMGVIEWER_IMG_COUNT_PER_PAGE + 1;
        snprintf(page, 8, "%d/%d", curr_page, total_page);
        lv_label_set_text(label_page, page);
        lv_obj_align(label_page, NULL, LV_ALIGN_IN_TOP_MID, 0, 11);
    }

    /*
        lv_obj_t * label_ph = lv_label_create(bg, NULL);
        lv_label_set_text_id(label_ph, WATCH_TEXT_ID_IMGVIEWER);
        lv_obj_add_style(label_ph, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
        lv_obj_align(label_ph, NULL, LV_ALIGN_IN_TOP_MID, 0, 15);
        lv_obj_set_click(label_ph, false);
    */

    return bg;
}

static void imgviewer_photo_item_create(lv_obj_t * par)
{
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    uint32_t photocnt = ext->photo_cnt - ext->photo_curr_page_start_index < IMGVIEWER_IMG_COUNT_PER_PAGE
                        ? ext->photo_cnt - ext->photo_curr_page_start_index : IMGVIEWER_IMG_COUNT_PER_PAGE;
    uint8_t line = photocnt % 3 == 0 ? photocnt / 3 : photocnt / 3 + 1;

    ext->img_page  = lv_page_create(par, NULL);
    lv_page_set_scrollbar_mode(ext->img_page, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_size(ext->img_page, LV_HOR_RES, 206);
    lv_obj_align(ext->img_page, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
    lv_obj_add_style(ext->img_page, LV_PAGE_PART_BG, &lv_watch_style_transp);
    lv_obj_add_style(ext->img_page, LV_PAGE_PART_SCROLLABLE, &lv_watch_style_transp);
    lv_obj_add_style(ext->img_page, LV_PAGE_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_watch_obj_add_element(ext->img_page);
    lv_obj_t * page_scrl = lv_page_get_scrl(ext->img_page);
    orignal_page_scrl_signal = lv_obj_get_signal_cb(page_scrl);
    lv_obj_set_signal_cb(page_scrl, imgviewer_item_page_scrl_signal);

    lv_obj_t * cont = lv_cont_create(ext->img_page, NULL);
    lv_obj_set_size(cont, LV_HOR_RES - 18, (IMGVIEWER_THUMBNAIL_H + 3) * line - 3);
    lv_obj_set_x(cont, 7);
    lv_cont_set_layout(cont, LV_LAYOUT_GRID);
    lv_obj_set_click(cont, false);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_0);
    lv_obj_set_style_local_pad_top(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 3);
    lv_watch_obj_add_element(cont);
    lv_page_glue_obj(cont, true);

    lv_obj_t * img[ext->photo_cnt];
    lv_obj_t * img_mask[ext->photo_cnt];
    uint32_t i = 0;
    uint32_t cnt = 0;
    imgviewer_photo_info_t * node = NULL;
    if(ext->time_order)
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
    else
        node = (imgviewer_photo_info_t *)_lv_ll_get_tail(photo_ll);
    while(node) {
        if(node->index < ext->photo_curr_page_start_index)  {
            if(ext->time_order)
                node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
            else
                node = (imgviewer_photo_info_t *)_lv_ll_get_prev(photo_ll, node);

            continue;
        }
        if(++cnt > IMGVIEWER_IMG_COUNT_PER_PAGE) {
            break;
        }

        lv_img_cache_invalidate_src(node->preview_src);
        img[i] = lv_img_create(cont, NULL);
        node->preview_src->header.w = IMGVIEWER_THUMBNAIL_W;
        node->preview_src->header.h = IMGVIEWER_THUMBNAIL_H;
        lv_img_set_src(img[i], (void *)node->preview_src);
        lv_watch_set_free_num(img[i], node->index);
        lv_watch_obj_add_element(img[i]);
        lv_page_glue_obj(img[i], true);
        lv_obj_set_adv_hittest(img[i], false);
        if(i == 0)
            orignal_img_signal = lv_obj_get_signal_cb(img[i]);
        if(ext->preview_mode == 0) {
            lv_obj_set_signal_cb(img[i], imgviewer_img_signal);
            lv_obj_add_protect(img[i], LV_PROTECT_PRESS_LOST);
        } else {
            img_mask[i] = lv_img_create(img[i], NULL);
            if(0 == node->selected) {
                lv_img_set_src(img_mask[i], ICON_IMGVIEWER_SELECT);
            } else {
                lv_img_set_src(img_mask[i], ICON_IMGVIEWER_SELECTED);
            }
            lv_watch_set_free_num(img_mask[i], node->index);
            lv_watch_obj_add_element(img_mask[i]);
            lv_obj_set_click(img_mask[i], true);
            lv_page_glue_obj(img_mask[i], true);
            lv_obj_set_signal_cb(img_mask[i], imgviewer_img_signal);
            lv_obj_add_protect(img_mask[i], LV_PROTECT_PRESS_LOST);
            lv_obj_set_adv_hittest(img_mask[i], false);
        }
        if(ext->time_order)
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        else
            node = (imgviewer_photo_info_t *)_lv_ll_get_prev(photo_ll, node);

        i++;
    }
    lv_obj_t * img_set = lv_obj_get_child_back(par, NULL);
    if(ext->preview_mode == 1) {
        lv_img_set_src(img_set, ICON_IMGVIEWER_DELETE_BLUE);
        lv_obj_set_event_cb(img_set, imgviewer_delete_event_cb);
    } else {
        lv_img_set_src(img_set, ICON_IMGVIEWER_MORE_SETTING);
        lv_obj_set_event_cb(img_set, imgviewer_setting_event_cb);
    }
    lv_obj_align(img_set, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
}

static void imgviewer_tabview_event_cb(lv_obj_t * tabview, lv_event_t e)
{
     if(e == LV_EVENT_VALUE_CHANGED)
     {
        lv_imgviewer_obj_ext_t * imgviewer_ext = imgviewer_get_ext();

        uint32_t id_cur = lv_watch_tabview_get_tab_act(tabview);
        uint32_t id_prev = *(uint32_t *) lv_event_get_data();

        imgviewer_ext->photo_tabview_curr_index += (id_cur - id_prev);
        imgviewer_ext->photo_curr_page_start_index =
                (imgviewer_ext->photo_tabview_curr_index / IMGVIEWER_IMG_COUNT_PER_PAGE) * IMGVIEWER_IMG_COUNT_PER_PAGE;

        if(imgviewer_ext->photo_cnt > IMGVIEWER_TAB_COUNT) {
            imgviewer_view_photo_tabview_create(imgviewer_ext->photo_tabview_curr_index);
        }
     }
}

static void imgviewer_view_photo_tabview_create(uint32_t img_index)
{
    lv_imgviewer_obj_ext_t * imgviewer_ext = imgviewer_get_ext();
    lv_obj_t * tab;
    lv_obj_t * img_view;
    uint32_t tab_cnt = 0;
    uint16_t tab_act = 0;
    imgviewer_photo_info_t * node = NULL;

    printf("%s: img_index=%d\n", __FUNCTION__, img_index);

    lv_obj_t * obj = imgviewer_get_obj();
    lv_obj_clean(obj);
    lv_obj_t  * tabview = lv_watch_tabview_create(obj);
    LV_ASSERT_MEM(tabview);
    if(tabview == NULL) return;

    lv_obj_set_size(tabview, LV_HOR_RES, LV_VER_RES);
    lv_obj_add_style(tabview, LV_TABVIEW_PART_TAB_BG, &lv_style_transp_tight);
    lv_obj_set_event_cb(tabview, imgviewer_tabview_event_cb);
    lv_watch_tabview_set_edge_flash(tabview, true);
    imgviewer_ext->tabview = tabview;

    if(img_index > 0) {
        imgviewer_ext->photo_tabview_start_index = img_index - 1;
        tab_act = 1;
    } else {
        imgviewer_ext->photo_tabview_start_index = img_index;
        tab_act = 0;
    }
    imgviewer_ext->photo_tabview_curr_index = img_index;

    if(imgviewer_ext->time_order)
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
    else
        node = (imgviewer_photo_info_t *)_lv_ll_get_tail(photo_ll);
    while(node) {
        if(node->index < imgviewer_ext->photo_tabview_start_index)  {
            if(imgviewer_ext->time_order)
                node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
            else
                node = (imgviewer_photo_info_t *)_lv_ll_get_prev(photo_ll, node);

            continue;
        }
        if(++tab_cnt > IMGVIEWER_TAB_COUNT) {
            break;
        }

        lv_img_cache_invalidate_src(node->preview_src);
        if((node->img_w * 4 < LV_HOR_RES) || (node->img_h * 4 < LV_VER_RES)) {
            node->preview_src->header.w = node->img_w;
            node->preview_src->header.h = node->img_h;
        } else {
            node->preview_src->header.w = LV_HOR_RES;
            node->preview_src->header.h = LV_VER_RES;
        }
        tab = lv_watch_tabview_add_tab(tabview);
        lv_obj_set_height(lv_page_get_scrl(tab), 240);

        lv_obj_t * img_bg = lv_img_create(tab, NULL);
        lv_img_set_src(img_bg, ICON_IMGVIEWER_TABVIEW_BG);
        lv_obj_set_size(img_bg, LV_HOR_RES, LV_VER_RES);
        lv_watch_set_free_num(img_bg, node->index);

        img_view = lv_img_create(img_bg, NULL);
        //lv_img_set_zoom(img_view, 240/node->preview_src->header.w * 256);
        lv_img_set_src(img_view, node->preview_src);
        lv_watch_set_free_num(img_view, node->index);
        lv_obj_set_adv_hittest(img_view, false);
        lv_obj_set_pos(lv_page_get_scrl(tab), 0, 0);
        lv_obj_align(img_view, NULL, LV_ALIGN_CENTER, 0, 0);
        imgviewer_view_photo_opt_create(img_view);
        if(imgviewer_ext->time_order)
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        else
            node = (imgviewer_photo_info_t *)_lv_ll_get_prev(photo_ll, node);
    }

    lv_watch_tabview_set_tab_act(tabview, tab_act, false);
}

static void imgviewer_view_photo_opt_create(lv_obj_t * img_view)
{
    lv_obj_t * par = lv_obj_get_parent(img_view);

    lv_obj_t * img_back = lv_img_create(par, NULL);
    lv_img_set_src(img_back, ICON_IMGVIEWER_BACK);
    lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_click(img_back, true);
    lv_obj_set_adv_hittest(img_back, false);
    lv_watch_set_free_num(img_back, IMGVIEWER_BACK_VIEW_PHOTO);
    lv_obj_set_event_cb(img_back, imgviewer_back_event_cb);

    lv_obj_t * img_share = lv_img_create(par, NULL);
    lv_img_set_src(img_share, ICON_IMGVIEWER_SHARE);
    lv_obj_align(img_share, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
    lv_obj_set_click(img_share, true);
    lv_obj_set_adv_hittest(img_share, false);
    lv_watch_set_free_num(img_share, lv_obj_get_user_data(img_view).user_num);
    lv_obj_set_event_cb(img_share, imgviewer_share_event_cb);

    lv_obj_t * img_del = lv_img_create(par, NULL);
    lv_img_set_src(img_del, ICON_IMGVIEWER_DELETE_GRAY);
    lv_obj_align(img_del, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
    lv_obj_set_click(img_del, true);
    lv_obj_set_adv_hittest(img_del, false);
    lv_obj_set_event_cb(img_del, imgviewer_delete_event_cb);
}

static lv_obj_t * imgviewer_setting_create(lv_obj_t * activity_obj)
{
    lv_obj_t * bg;
    lv_obj_t * obj;

    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_IMGVIEWER_SETTING;
        activity_ext.create = imgviewer_setting_create;
        activity_ext.prepare_destory = imgviewer_setting_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    bg = lv_cont_create(obj, NULL);
    lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));

    lv_obj_t * img_back = lv_img_create(bg, NULL);
    lv_img_set_src(img_back, ICON_IMGVIEWER_BACK_BLUE);
    lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_watch_set_free_num(img_back, IMGVIEWER_BACK_SETTING);
    lv_obj_set_click(img_back, true);
    lv_obj_set_adv_hittest(img_back, false);
    lv_obj_set_event_cb(img_back, imgviewer_back_event_cb);

    lv_obj_t * label = lv_label_create(bg, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_IMGVIEWER_SETTING);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLUE);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);
    lv_obj_set_click(label, false);

    //cont2 - del multi
    lv_obj_t * cont2 = lv_cont_create(bg, NULL);
    lv_obj_set_size(cont2, 212, 54);
    lv_obj_set_style_local_bg_color(cont2, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(cont2, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_DPI / 15);
    lv_obj_align(cont2, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_watch_set_free_num(cont2, 3);
    lv_obj_set_event_cb(cont2, imgviewer_setting_opt_event_cb);

    lv_obj_t * label_delmulti = lv_label_create(cont2, NULL);
    lv_label_set_text_id(label_delmulti, WATCH_TEXT_ID_DEL_MULTI_PHOTO);
    lv_obj_add_style(label_delmulti, LV_LABEL_PART_MAIN, &lv_watch_font20);

    lv_obj_t * label_right2 = lv_label_create(cont2, NULL);
    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(bg)) {
        lv_label_set_text(label_right2, LV_SYMBOL_LEFT);
    } else {
        lv_label_set_text(label_right2, LV_SYMBOL_RIGHT);
    }
    lv_obj_add_style(label_right2, LV_LABEL_PART_MAIN,  &lv_watch_font20);
    lv_obj_set_style_local_text_font(label_right2, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);

    //cont3 - del all
    lv_obj_t * cont3 = lv_cont_create(bg, cont2);
    lv_obj_set_style_local_bg_color(cont3, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(cont3, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_DPI / 15);
    lv_obj_align(cont3, cont2, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_watch_set_free_num(cont3, 4);
    lv_obj_set_event_cb(cont3, imgviewer_setting_opt_event_cb);

    lv_obj_t * label_delall = lv_label_create(cont3, label_delmulti);
    lv_label_set_text_id(label_delall, WATCH_TEXT_ID_DEL_ALL_PHOTO);

    lv_obj_t * label_right3 = lv_label_create(cont3, label_right2);

    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(bg)) {
        lv_obj_align(label_delmulti, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
        lv_obj_align(label_right2, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
        lv_obj_align(label_delall, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
        lv_obj_align(label_right3, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
    } else {
        lv_obj_align(label_delmulti, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
        lv_obj_align(label_right2, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
        lv_obj_align(label_delall, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
        lv_obj_align(label_right3, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
    }

    return obj;
}

static void imgviewer_setting_delete_create(lv_obj_t * par, uint8_t del_one)
{
    lv_obj_t * bg ;
    lv_obj_t * img_to_del ;
    uint8_t num = 0;

    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    if(del_one) {
        bg = lv_cont_create(par, NULL);
        lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
        lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));
        if(ext->preview_mode == 1 && ext->selected_cnt == 1) num = 2;
        else num = 1;
    } else {
        bg = lv_img_create(par, NULL);
        lv_img_set_src(bg, ICON_IMGVIEWER_DEL_ALL_BG);
        lv_obj_set_click(bg, true);
        if(ext->preview_mode == 1) num = 2;
        else num = 3;   //del all
    }
    ext->del_bg = bg;
    imgviewer_photo_info_t * node = imgviewer_get_del_img_node(par, num);
    lv_img_cache_invalidate_src(node->preview_src);

    if((node->img_w * 4 < IMGVIEWER_DEL_PREVIEW_W) || (node->img_h * 4 < IMGVIEWER_DEL_PREVIEW_H)) {
        node->preview_src->header.w = node->img_w;
        node->preview_src->header.h = node->img_h;
    } else {
        node->preview_src->header.w = IMGVIEWER_DEL_PREVIEW_W;
        node->preview_src->header.h = IMGVIEWER_DEL_PREVIEW_H;
    }

    lv_obj_t * img_bg = lv_img_create(bg, NULL);
    lv_img_set_src(img_bg, ICON_IMGVIEWER_DEL_PREVIEW_BG);
    lv_obj_set_size(img_bg, IMGVIEWER_DEL_PREVIEW_W, IMGVIEWER_DEL_PREVIEW_H);
    lv_watch_set_free_num(img_bg, node->index);
    lv_obj_align(img_bg, NULL, LV_ALIGN_IN_TOP_MID, 0, 16);

    img_to_del = lv_img_create(img_bg, NULL);
    lv_img_set_src(img_to_del, node->preview_src);
    lv_watch_set_free_num(img_to_del, node->index);
    lv_obj_align(img_to_del, NULL, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label_q = lv_label_create(bg, NULL);
    if(3 != num)
        lv_label_set_text_id(label_q, WATCH_TEXT_ID_DEL_Q);
    else
        lv_label_set_text_id(label_q, WATCH_TEXT_ID_DEL_ALL_PHOTO_Q);
    if(lv_lang_act() == LANG_CH)
        lv_obj_add_style(label_q, LV_LABEL_PART_MAIN, &lv_watch_font30_black);
    else
        lv_obj_add_style(label_q, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_obj_align(label_q, img_bg, LV_ALIGN_OUT_BOTTOM_MID, 0, 15);

    lv_obj_t * btn_cancel = lv_btn_create(bg, NULL);
    lv_obj_set_size(btn_cancel, 100, 56);
    lv_obj_set_style_local_bg_color(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 30);
    lv_obj_set_style_local_bg_color(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 30);
    lv_obj_align(btn_cancel, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, -10);
    lv_obj_set_event_cb(btn_cancel, imgviewer_cancel_btn_event_cb);
    lv_watch_set_free_num(btn_cancel, num);
    lv_obj_t * label_cancel = lv_label_create(btn_cancel, NULL);
    lv_label_set_text_id(label_cancel, WATCH_TEXT_ID_CANCEL);
    lv_obj_add_style(label_cancel, LV_LABEL_PART_MAIN, &lv_watch_font30);

    lv_obj_t * btn_ok = lv_btn_create(bg, btn_cancel);
    lv_obj_set_style_local_bg_color(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 30);
    lv_obj_set_style_local_bg_color(btn_ok, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 30);
    lv_obj_align(btn_ok, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -10);
    lv_watch_set_free_num(btn_ok, num);
    lv_obj_set_event_cb(btn_ok, imgviewer_ok_btn_event_cb);
    lv_obj_t * label_ok = lv_label_create(btn_ok, label_cancel);
    lv_label_set_text_id(label_ok, WATCH_TEXT_ID_OK);
}

static void imgviewer_recreate_preview(bool reset_preview_mode)
{
    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    ext->timer = NULL;
    lv_obj_t * obj = imgviewer_get_obj();
    lv_obj_clean(obj);
    imgviewer_photo_info_t * node = NULL;
    _LV_LL_READ(*photo_ll, node) {
        lv_img_cache_invalidate_src(node->preview_src);
    }
    lv_obj_t * bg = imgviewer_photo_bg_create(obj);
    if(reset_preview_mode && ext->preview_mode == 1) {
        ext->preview_mode = 0;
        imgviewer_reset_photo_ll_selected();
    }
    if(ext->photo_cnt > 0) {
        imgviewer_photo_item_create(bg);
    }
}

static void imgviewer_delete_success_timer_cb(void * param)
{
    if(lv_watch_get_activity_obj(ACT_ID_IMGVIEWER) == NULL)
        return;
    imgviewer_recreate_preview(true);
}

static void imgviewer_delete_success_create()
{
    lv_obj_t * cont_bg = lv_cont_create(imgviewer_get_obj(), NULL);
    lv_obj_set_size(cont_bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_add_style(cont_bg, LV_CONT_PART_MAIN, &lv_watch_black_tight);

    lv_obj_t * img_suc = lv_img_create(cont_bg, NULL);
    lv_img_set_src(img_suc, ICON_OK);
    lv_obj_align(img_suc, NULL, LV_ALIGN_IN_TOP_MID, 0, 30);

    lv_obj_t * label = lv_label_create(cont_bg, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text_id(label, WATCH_TEXT_ID_DEL_SUCCESS);
    lv_obj_align(label, img_suc, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    ext->timer = Hal_Timer_Start(1000, imgviewer_delete_success_timer_cb, NULL, false);
}

static void imgviewer_photo_share_confirm_create(lv_obj_t * par, voice_msg_contact_t * contact, void * src)
{
    char str[64];

    lv_obj_t * bg = lv_cont_create(par, NULL);
    lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));

    lv_obj_t * img = lv_img_create(bg, NULL);
    lv_img_set_src(img, src);
    lv_obj_align(img, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * label = lv_label_create(bg, NULL);
    memset(str, 0, 64);
    if(LANG_CH == lv_lang_act()) {
        snprintf(str, 64, "%s%s?", (char *)lv_lang_get_text(WATCH_TEXT_ID_SEND_TO), contact->name);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30_black);
    } else {
        snprintf(str, 64, "%s %s?", (char *)lv_lang_get_text(WATCH_TEXT_ID_SEND_TO), contact->name);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    }
    lv_label_set_text(label, str);
    lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
    lv_obj_set_width(label, LV_HOR_RES - 20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(label, img, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_obj_t * btn_cancel = lv_btn_create(bg, NULL);
    lv_obj_set_size(btn_cancel, 100, 48);
    lv_obj_set_style_local_bg_color(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 30);
    lv_obj_set_style_local_bg_color(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 30);
    lv_obj_align(btn_cancel, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, -10);
    lv_obj_set_event_cb(btn_cancel, imgviewer_share_cancel_btn_event_cb);
    lv_obj_t * label_cancel = lv_label_create(btn_cancel, NULL);
    lv_label_set_text_id(label_cancel, WATCH_TEXT_ID_CANCEL);
    lv_obj_add_style(label_cancel, LV_LABEL_PART_MAIN, &lv_watch_font30);

    lv_obj_t * btn_ok = lv_btn_create(bg, btn_cancel);
    lv_obj_set_style_local_bg_color(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 30);
    lv_obj_set_style_local_bg_color(btn_ok, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 30);
    lv_obj_align(btn_ok, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -10);
    lv_watch_set_free_ptr(btn_ok, contact);
    lv_obj_set_event_cb(btn_ok, imgviewer_share_ok_btn_event_cb);
    lv_obj_t * label_ok = lv_label_create(btn_ok, label_cancel);
    lv_label_set_text_id(label_ok, WATCH_TEXT_ID_OK);
}

static void imgviewer_share_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_obj_t * bg_obj = lv_obj_get_parent(btn);
        lv_obj_del(bg_obj);
    }
}

static void imgviewer_share_ok_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_obj_t * imgviewer_act_obj = lv_watch_get_activity_obj(ACT_ID_IMGVIEWER);
        lv_obj_t * camera_act_obj = lv_watch_get_activity_obj(ACT_ID_CAMERA);
        if(imgviewer_act_obj == NULL && camera_act_obj) {
            lv_obj_t * watch_obj = NULL;
            lv_camera_obj_ext_t  * ext = NULL;
            lv_watch_get_child_obj(camera_act_obj, lv_watch_obj_signal, &watch_obj);
            if(watch_obj)
                ext = lv_obj_get_ext_attr(watch_obj);
            lv_img_dsc_t * src = (lv_img_dsc_t *)lv_img_get_src(ext->img);
            voice_msg_send_photo((void *)(src->data), src->data_size, (voice_msg_contact_t *)lv_obj_get_user_data(btn).user_data);

        } else if(imgviewer_act_obj) {
            imgviewer_photo_info_t * node = NULL;
            node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
            while(node) {
                if(node->selected == 1) {
                    lv_fs_file_t file;
                    void * buf;
                    lv_fs_res_t res = lv_fs_open(&file, (const char *)node->preview_src->data, LV_FS_MODE_RD);
                    if(res == LV_FS_RES_OK) {
                        uint32_t rn;
                        uint32_t size;
                        lv_fs_size(&file, &size);
                        buf = lv_mem_alloc(size);
                        memset(buf, 0, size);
                        lv_fs_read(&file, buf, size, &rn);
                        lv_fs_close(&file);
                        voice_msg_send_photo(buf, size, (voice_msg_contact_t *)lv_obj_get_user_data(btn).user_data);
                        lv_mem_free(buf);
                    }
                    node->selected = 0;
                    break;
                }
                node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
            }
        }
    }
}

static void imgviewer_ok_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
        uint8_t type = lv_obj_get_user_data(btn).user_num;
        lv_obj_del(lv_obj_get_parent(btn));
        ext->del_bg = NULL;
        if(lv_watch_get_activity_obj(ACT_ID_IMGVIEWER_SETTING))
            lv_obj_del(lv_watch_get_activity_obj(ACT_ID_IMGVIEWER_SETTING));
        lv_obj_t * obj = imgviewer_get_obj();
        lv_obj_clean(obj);
        imgviewer_photo_delete(type);
        imgviewer_delete_success_create();
    }
}

static void imgviewer_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
        uint32_t img_num = lv_obj_get_user_data(lv_obj_get_child_back(lv_obj_get_parent(btn), NULL)).user_num;
        uint8_t btn_num = lv_obj_get_user_data(btn).user_num;
        imgviewer_photo_info_t * node = NULL;
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->index == img_num) {
                lv_img_cache_invalidate_src(node->preview_src);
                if(btn_num == 1) {
                    if((node->img_w * 4 < LV_HOR_RES) || (node->img_h * 4 < LV_VER_RES)) {
                        node->preview_src->header.w = node->img_w;
                        node->preview_src->header.h = node->img_h;
                    } else {
                        node->preview_src->header.w = LV_HOR_RES;
                        node->preview_src->header.h = LV_VER_RES;
                    }
                    node->selected = 0;
                } else {
                    node->preview_src->header.w = IMGVIEWER_THUMBNAIL_W;
                    node->preview_src->header.h = IMGVIEWER_THUMBNAIL_H;
                }
                break;
            }
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
        lv_obj_del(lv_obj_get_parent(btn));
        ext->del_bg = NULL;
        if(ext->preview_mode == 1) {
            ext->preview_mode = 0;
            imgviewer_reset_photo_ll_selected();
            lv_obj_t * bg = lv_obj_get_parent(ext->img_page);
            lv_obj_del(ext->img_page);
            imgviewer_photo_item_create(bg);
        }
    }
}

static void imgviewer_back_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        uint8_t num = lv_obj_get_user_data(obj).user_num;
        printf("imgviewer_back_signal: num=%d\n", num);
        if(num == IMGVIEWER_BACK_ORDER_SET) {
            lv_obj_t * par_obj = lv_obj_get_parent(obj);
            lv_obj_del(par_obj);
            imgviewer_set_photo_order_label_text();
        } else if(num == IMGVIEWER_BACK_QUIT) {
            lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
            if(ext->preview_mode == 0) {//quit imgviewer app
                imgviewer_destory(true);
                lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_IMGVIEWER);
                if(activity_obj) lv_obj_del(activity_obj);
            } else {
                ext->preview_mode = 0;
                imgviewer_reset_photo_ll_selected();
                lv_obj_t * img = lv_obj_get_child_back(lv_obj_get_child(lv_obj_get_child(ext->img_page, NULL), NULL), NULL);
                while(img) {
                    lv_obj_del(lv_obj_get_child(img, NULL));
                    lv_obj_set_signal_cb(img, imgviewer_img_signal);
                    img = lv_obj_get_child_back(lv_obj_get_child(lv_obj_get_child(ext->img_page, NULL), NULL), img);
                }
                lv_obj_t * img_set = lv_obj_get_child_back(lv_obj_get_parent(ext->img_page), NULL);
                lv_img_set_src(img_set, ICON_IMGVIEWER_MORE_SETTING);
                lv_obj_set_event_cb(img_set, imgviewer_setting_event_cb);
            }
        } else if(num == IMGVIEWER_BACK_SETTING) {
            lv_obj_t * activity_obj = lv_watch_get_top_activity_obj();
            if(activity_obj)
                lv_obj_del(activity_obj);
        } else if(num == IMGVIEWER_BACK_VIEW_PHOTO) {
            imgviewer_recreate_preview(true);
            imgviewer_reset_photo_ll_selected();
        } else if(num == IMGVIEWER_BACK_SEL_SHARE) {
            lv_obj_del(lv_obj_get_parent(obj));
        } else {
            lv_obj_del(lv_obj_get_child(lv_obj_get_parent(obj), NULL));
            lv_watch_set_free_num(obj, IMGVIEWER_BACK_SEL_SHARE);
            lv_label_set_text_id(lv_obj_get_child_back(lv_obj_get_parent(obj), obj), WATCH_TEXT_ID_SHARE_TO);
        }
    }
}
static void imgviewer_setting_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        imgviewer_setting_create(NULL);
    }
}

#if USE_LV_BLUETOOTH != 0
static void imgviewer_share_to_voice_msg_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    lv_obj_del(lv_obj_get_parent(btn));
    voice_msg_create(NULL, 2, NULL);
}

static void imgviewer_share_select_dev_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    char * file_path;
    lv_obj_t * imgviewer_act_obj = lv_watch_get_activity_obj(ACT_ID_IMGVIEWER);
    lv_obj_t * camera_act_obj = lv_watch_get_activity_obj(ACT_ID_CAMERA);
    if(imgviewer_act_obj == NULL && camera_act_obj) {
        file_path = (char *)lv_obj_get_user_data(btn).user_data;
    } else if(imgviewer_act_obj) {
        imgviewer_photo_info_t * info = (imgviewer_photo_info_t *)lv_obj_get_user_data(btn).user_data;
        lv_img_cache_invalidate_src(info->preview_src);
        file_path = (char *)info->preview_src->data;
        info->selected = 0;
    }
    bluetooth_send_file_mbox(file_path);
    uint8_t num = lv_obj_get_user_data(btn).user_num;
    bt_paired_dev_t * paired_list = bt_get_paired_list();
    struct bt_addr addr;
    memcpy(addr.bytes, paired_list[num].addr, 6);
    bt_send_file_pre(file_path, addr);
    lv_obj_t * bg = lv_obj_get_parent(lv_obj_get_parent(lv_obj_get_parent(btn)));
    lv_obj_del(bg);
}

static void imgviewer_share_to_bt_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    lv_obj_t * bg = lv_obj_get_parent(btn);
    if(bt_get_state() == false) {
        lv_obj_t * mbox = lv_msgbox_create(bg, NULL);
        lv_msgbox_set_text(mbox, (char *)lv_lang_get_text(WATCH_TEXT_ID_OPEN_BT_FIRST));
        lv_obj_set_size(mbox, LV_HOR_RES * 2 / 3, LV_VER_RES * 2 / 3);
        lv_obj_realign(mbox);
        lv_msgbox_set_anim_time(mbox, 0);
        lv_msgbox_start_auto_close(mbox, 2000);
        return;
    }
    if(bt_get_headset_connect()) {
        lv_obj_t * mbox = lv_msgbox_create(bg, NULL);
        lv_msgbox_set_text(mbox, (char *)lv_lang_get_text(WATCH_TEXT_ID_DISCNNECT_HEADSET));
        lv_obj_set_size(mbox, LV_HOR_RES * 2 / 3, LV_VER_RES * 2 / 3);
        lv_obj_realign(mbox);
        lv_msgbox_set_anim_time(mbox, 0);
        lv_msgbox_start_auto_close(mbox, 2000);
        return;
    }
    lv_obj_t * img_back = lv_obj_get_child_back(bg, NULL);
    lv_watch_set_free_num(img_back, IMGVIEWER_BACK_SEL_DEV);
    lv_obj_t * title = lv_obj_get_child_back(bg, img_back);
    lv_label_set_text_id(title, WATCH_TEXT_ID_SELECT_DEV);

    lv_obj_t * list = lv_list_create(bg, NULL);
    lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES - 40);
    lv_obj_align(list, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
    lv_page_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);
    lv_obj_add_style(list, LV_LIST_PART_BG, lv_obj_get_local_style(bg, LV_CONT_PART_MAIN));
    lv_obj_set_style_local_pad_inner(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, 7);
    lv_obj_set_style_local_pad_right(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, 7);
    lv_obj_set_style_local_pad_bottom(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, 5);
    lv_obj_add_style(list, LV_LIST_PART_SCROLLABLE, lv_obj_get_local_style(bg, LV_CONT_PART_MAIN));
    lv_obj_set_style_local_pad_inner(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 5);

    if(bt_get_paired_dev_cnt() == 0) {
        lv_obj_t * tip_label = lv_label_create(list, NULL);
        lv_obj_add_style(tip_label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
        lv_label_set_text_id(tip_label, WATCH_TEXT_ID_NO_DEV);
        lv_obj_align(tip_label, NULL, LV_ALIGN_CENTER, 0, 0);
    } else {
        bt_paired_dev_t * paired_list = bt_get_paired_list();
        lv_obj_t * dev_btn;
        uint8_t i = 0;
        bool b_find_phone = false;
        for(i = 0; i < bt_get_paired_dev_cnt(); i++) {
            if((paired_list[i].cod & COD_TYPE_PHONE) == COD_TYPE_PHONE) {
                dev_btn = lv_list_add_btn(list, NULL, (const char *)paired_list[i].name);
                lv_watch_set_free_ptr(dev_btn, lv_obj_get_user_data(btn).user_data);
                lv_watch_set_free_num(dev_btn, i);
                lv_obj_set_event_cb(dev_btn, imgviewer_share_select_dev_action);
                lv_obj_add_style(dev_btn, LV_BTN_PART_MAIN, lv_obj_get_local_style(btn, LV_BTN_PART_MAIN));
                lv_obj_add_style(lv_obj_get_child(dev_btn, NULL), LV_LABEL_PART_MAIN, &lv_watch_font20_black);
                b_find_phone = true;
            }
        }
        if(!b_find_phone) {
            lv_obj_t * tip_label = lv_label_create(list, NULL);
            lv_obj_add_style(tip_label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
            lv_label_set_text_id(tip_label, WATCH_TEXT_ID_NO_DEV);
            lv_obj_align(tip_label, NULL, LV_ALIGN_CENTER, 0, 0);
        }
    }
}
#endif

static void imgviewer_share_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        imgviewer_photo_info_t * node = NULL;
        node = (imgviewer_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->index == lv_obj_get_user_data(obj).user_num) {
                node->selected = 1;
                break;
            }
            node = (imgviewer_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
#if USE_LV_BLUETOOTH != 0
        imgviewer_select_share_way(lv_obj_get_parent(obj));
#else
        voice_msg_create(NULL, 2, NULL);
#endif
    }
}

static void imgviewer_delete_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
        if(ext->preview_mode == 1) {
            if(ext->selected_cnt == 0)return;
            else if(ext->selected_cnt == 1)
                imgviewer_setting_delete_create(lv_obj_get_parent(obj), 1);
            else
                imgviewer_setting_delete_create(lv_obj_get_parent(obj), 0);
        } else {
            imgviewer_setting_delete_create(lv_obj_get_parent(obj), 1);
        }
    }
}

static lv_res_t imgviewer_img_signal(lv_obj_t * obj, lv_signal_t sign, void * param)
{
    lv_res_t res = LV_RES_OK;
    res = orignal_img_signal(obj, sign, param);
    if(res != LV_RES_OK) return res;

    lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
    if(LV_SIGNAL_PRESSED == sign) {
        ext->pressing = true;
    } else if((true == ext->pressing) && (sign == LV_SIGNAL_RELEASED)) {
        ext->pressing = false;
        if(ext->preview_mode == 0)
            imgviewer_view_photo_tabview_create(lv_obj_get_user_data(obj).user_num);
        else {
            if(strcmp(lv_img_get_src(obj), ICON_IMGVIEWER_SELECT) == 0) {
                imgviewer_set_photo_ll_selected(lv_obj_get_user_data(obj).user_num, 1);
                lv_img_set_src(obj, ICON_IMGVIEWER_SELECTED);
                ext->selected_cnt++;
            } else {
                imgviewer_set_photo_ll_selected(lv_obj_get_user_data(obj).user_num, 0);
                lv_img_set_src(obj, ICON_IMGVIEWER_SELECT);
                ext->selected_cnt--;
            }
        }
    }
    return res;
}

static lv_res_t imgviewer_item_page_scrl_signal(lv_obj_t * page_scrl, lv_signal_t sign, void * param)
{
    lv_res_t res;

    /* Include the ancient signal function */
    res = orignal_page_scrl_signal(page_scrl, sign, param);
    if(res != LV_RES_OK) return res;

    if(LV_SIGNAL_COORD_CHG == sign) {
        lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
        if(true == ext->pressing) {
            ext->pressing = false;
        }
    }
    return LV_RES_OK;
}

static void imgviewer_setting_opt_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        uint8_t num = lv_obj_get_user_data(obj).user_num;
        lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();
            if(num == 3) {   //del multi photos
                lv_obj_t * activity_obj = lv_watch_get_top_activity_obj();
                lv_obj_t * bg = lv_obj_get_parent(ext->img_page);
                if(activity_obj) {
                    lv_obj_del(activity_obj);
                }
                lv_obj_del(ext->img_page);
                ext->preview_mode = 1;
                imgviewer_photo_item_create(bg);
            } else if(num == 4) {   //del all photos
                lv_obj_t * par = lv_obj_get_parent(lv_obj_get_parent(obj));
                imgviewer_setting_delete_create(par, 0);
            }
    }
}

static void imgviewer_prev_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();

        if(ext->photo_curr_page_start_index >= IMGVIEWER_IMG_COUNT_PER_PAGE) {
            ext->photo_curr_page_start_index -= IMGVIEWER_IMG_COUNT_PER_PAGE;
            imgviewer_recreate_preview(false);
        }
        printf("%s: prev - photo_curr_page_start_index=%d\n", __FUNCTION__, ext->photo_curr_page_start_index);
    }
}

static void imgviewer_next_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_imgviewer_obj_ext_t * ext = imgviewer_get_ext();

        if(ext->photo_cnt - ext->photo_curr_page_start_index > IMGVIEWER_IMG_COUNT_PER_PAGE) {
            ext->photo_curr_page_start_index += IMGVIEWER_IMG_COUNT_PER_PAGE;
            imgviewer_recreate_preview(false);
        }
        printf("%s: next - photo_curr_page_start_index=%d\n", __FUNCTION__, ext->photo_curr_page_start_index);
    }
}

static int imgviewer_check_img_size(char * fullname, uint32_t * img_w, uint32_t * img_h)
{
    lv_img_header_t header;
    lv_fs_file_t file;
    uint32_t file_size = 0;
    uint32_t max_w = IMGVIEWER_MAX_W;
    uint32_t max_h = IMGVIEWER_MAX_H;
    int ret = -1;

    lv_fs_res_t fs_res = lv_fs_open(&file, fullname, LV_FS_MODE_RD);
    if(LV_FS_RES_OK != fs_res) {
        return ret;
    }

    fs_res = lv_fs_size(&file, &file_size);
    lv_fs_close(&file);
    if((LV_FS_RES_OK != fs_res) || (file_size > IMGVIEWER_MAX_FILE_SIZE)) {
        return ret;
    }

    const char * ext = lv_fs_get_ext(lv_fs_get_last(fullname));
    if(0 == strcmp(IMGVIEWER_EXT_NAME, ext)) {  //jpg
        max_w *= 8;
        max_h *= 8;
    }

    lv_res_t lv_res = lv_img_decoder_get_info(fullname, &header);
    if((LV_RES_OK == lv_res)
            && (header.w <= max_w)
            && (header.h <= max_h)
            && (header.w >= IMGVIEWER_THUMBNAIL_W/4)
            && (header.h >= IMGVIEWER_THUMBNAIL_H/4)) {
        *img_w = header.w;
        *img_h = header.h;
        ret = 0;
    }

    return ret;
}

static void imgviewer_get_img_info(char * path, ui_file_t * pfile)
{
    printf("%s: path=%s\n", __FUNCTION__, path);

    if(img_file_cnt >= IMGVIEWER_MAX_COUNT) {
        return;
    }

#ifndef BUILD_IN_PC_SIMULATOR
    lv_fs_dir_t dir;
    char * filename = (char *)lv_mem_alloc(IMGVIEWER_PATH_MAX);
    char * newpath = NULL;
    const char * extname = NULL;
    int ret = LV_FS_RES_OK;
    char fullname[IMGVIEWER_PATH_LEN];
    uint32_t img_w = 0;
    uint32_t img_h = 0;

    memset(&dir, 0, sizeof(dir));
    ret = lv_fs_dir_open(&dir, path);
    if(ret == LV_FS_RES_OK) {
        //printf("opendir OK handle:0x%x\n", *(uint32_t *)dir.dir_d);
        while((ret & 0x0F) == LV_FS_RES_OK) {
            memset(filename, 0, IMGVIEWER_PATH_MAX);
            ret = lv_fs_dir_read(&dir, filename);
            if((ret & 0x0F) == LV_FS_RES_OK) {
                //printf("-->read file name:%s attr:%x\n", filename, (ret & 0xF0));
                switch((ret & 0xF0)) {
                    case IMGVIEWER_FILE_ARC:
                        extname = lv_fs_get_ext(filename);
                        //printf("-->ext name:%s\n", extname);
                        if((0 == strcmp(IMGVIEWER_EXT_NAME, extname)) || (0 == strcmp(IMGVIEWER_EXT_NAME_SUB, extname))) {
                            //printf("cmp ok-->path:%s, filename%s\n", path, filename);

                            memset(fullname, 0, IMGVIEWER_PATH_LEN);
                            strncpy(fullname, path, (strlen(path) - 1)); //without *
                            strcat(fullname, filename);

                            if(0 == imgviewer_check_img_size(fullname, &img_w, &img_h)) {
#if IMGVIEWER_WRITE_PATH_FILE
                                imgviewer_store_img_info_to_global(path, filename, pfile);
#else
                                imgviewer_load_one_photo_path(path, filename, img_w, img_h);
#endif
                            }
                        }
                        break;
                    case IMGVIEWER_FILE_DIR:
                        newpath = (char *)lv_mem_alloc(strlen(path) + strlen(filename) + 5);
                        memset(newpath, 0, strlen(path) + strlen(filename) + 5);
                        memcpy(newpath, path, (strlen(path) - 1)); //without *
                        strcat(newpath, filename);
                        strcat(newpath, "\\*");
                        imgviewer_get_img_info(newpath, pfile);
                        lv_mem_free(newpath);
                        break;
                    case IMGVIEWER_FILE_HID:
                    default:
                        break;
                }
            }
        }
        lv_fs_dir_close(&dir);
    } else {
        printf("opendir %s failed!!!\n", path);
    }
    lv_mem_free(filename);

#else   //BUILD_IN_PC_SIMULATOR

    imgviewer_check_img_size(NULL, NULL, NULL);

#if IMGVIEWER_WRITE_PATH_FILE
    imgviewer_store_img_info_to_global("C:/jpg/*", "42511560800.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "42531680801.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "42571827202.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "big.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "icon_ant_small.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "icon_audio_player_background.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "icon_audio_player_checkbox.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "icon_bear.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "icon_check.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "icon_del_all_bg.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "icon_dial_clock.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "img_bg_truecolor.jpg", pfile);
    imgviewer_store_img_info_to_global("C:/jpg/*", "pngbar.jpg", pfile);

#if 0
    imgviewer_store_img_info_to_global("C:/png/dial_bear/*", "icon_bear.png", pfile);
    imgviewer_store_img_info_to_global("C:/png/dial_dolphin/*", "icon_dolphin.png", pfile);
    imgviewer_store_img_info_to_global("C:/png/dial_rocket/*", "icon_rocket.png", pfile);
    imgviewer_store_img_info_to_global("C:/png/album/*", "icon_no_photo.png", pfile);
    imgviewer_store_img_info_to_global("C:/png/phone_portrait/*", "icon_ant_small.png", pfile);
    imgviewer_store_img_info_to_global("C:/png/dial/*", "icon_dial_clock.png", pfile);
    imgviewer_store_img_info_to_global("C:/png/audio_player/*", "icon_audio_player_checkbox.png", pfile);
#endif
#endif

#endif
}

#if IMGVIEWER_WRITE_PATH_FILE
static img_info_t * imgviewer_get_img_info_from_filesystem(uint32_t index, bool mode)
{
    ui_file_t * pfile = NULL;
    uint32_t ret_len = 0;
    int ret = LV_FS_RES_OK;
    char path[6];

    printf("%s: index=%d\n", __FUNCTION__, index);
    strncpy(path, IMGVIEWER_ROOT_PATH, 6);

#ifndef BUILD_IN_PC_SIMULATOR
    lv_fs_dir_t dir;
    memset(&dir, 0, sizeof(dir));
    ret = lv_fs_dir_open(&dir, path);
    if(ret != LV_FS_RES_OK) {
        printf("%s: opendir %s failed!\n", __FUNCTION__, path);
        img_file_cnt = 0;
        return NULL;
    }
    lv_fs_dir_close(&dir);
#endif

    pfile = lv_mem_alloc(sizeof(ui_file_t));
    memset(pfile, 0, sizeof(ui_file_t));
    ret = lv_fs_open(pfile, IMGVIEWER_PATH_FILE, LV_FS_MODE_RD);
    printf("ret is %d in %s\n", ret, __FUNCTION__);

    if(LV_FS_RES_OK != ret) {
        printf("%s is not exist!\n", IMGVIEWER_PATH_FILE);
        img_file_cnt = 0;
        if(true == mode) {
            lv_fs_close(pfile);
            printf("create %s\n", IMGVIEWER_PATH_FILE);
            memset(pfile, 0, sizeof(ui_file_t));
            ret = lv_fs_open(pfile, IMGVIEWER_PATH_FILE, LV_FS_MODE_WR);
            if(LV_FS_RES_OK != ret) {
                printf("%s create fail!\n", IMGVIEWER_PATH_FILE);
                lv_mem_free(pfile);
                return NULL;
            }
            lv_fs_close(pfile);
            memset(pfile, 0, sizeof(ui_file_t));
            ret = lv_fs_open(pfile, IMGVIEWER_PATH_FILE, LV_FS_MODE_RD | LV_FS_MODE_APPEND);
            if(LV_FS_RES_OK != ret) {
                printf("%s second open failed!\n", IMGVIEWER_PATH_FILE);
                LV_ASSERT_MEM(pfile->file_d);
                lv_mem_free(pfile);
                return NULL;
            }
            lv_fs_seek(pfile, 0);
            lv_fs_write(pfile, &img_file_cnt, sizeof(uint32_t), &ret_len);
            if(0 == ret_len) {
                printf("write img_file_cnt in sdfs is wrong!\n");
                lv_fs_close(pfile);
                lv_mem_free(pfile);
                return NULL;
            }

            imgviewer_get_img_info(path, pfile);    //get img info

            lv_fs_seek(pfile, 0);
            lv_fs_read(pfile, &img_file_cnt, sizeof(uint32_t), &ret_len);
            if(sizeof(uint32_t) != ret_len) {
                printf("read img_file_cnt in sdfs is wrong!\n");
                lv_fs_close(pfile);
                lv_mem_free(pfile);
                return NULL;
            }

            lv_fs_seek(pfile, index * IMGVIEWER_PATH_MAX * 2 + sizeof(uint32_t));
            memset(&img_info_global, 0, sizeof(img_info_t));  // read data
            lv_fs_read(pfile, img_info_global.path, IMGVIEWER_PATH_MAX, &ret_len);
            if(IMGVIEWER_PATH_MAX != ret_len) {
                printf("read path in sdfs is wrong!\n");
                lv_fs_close(pfile);
                lv_mem_free(pfile);
                return NULL;
            }

            lv_fs_seek(pfile, index * IMGVIEWER_PATH_MAX * 2 + IMGVIEWER_PATH_MAX + sizeof(uint32_t));
            lv_fs_read(pfile, img_info_global.filename, IMGVIEWER_PATH_MAX, &ret_len);
            lv_fs_close(pfile);
            lv_mem_free(pfile);
            if(IMGVIEWER_PATH_MAX != ret_len) {
                printf("read filename in sdfs is wrong!\n");
                return NULL;
            }
        }
        else {
            lv_mem_free(pfile);
        }
        return NULL;
    } else {
        printf("%s is exist!\n", IMGVIEWER_PATH_FILE);
        if(true == mode) {
            lv_fs_close(pfile);
            printf("create %s\n", IMGVIEWER_PATH_FILE);
            memset(pfile, 0, sizeof(ui_file_t));
            ret = lv_fs_open(pfile, IMGVIEWER_PATH_FILE, LV_FS_MODE_WR);
            if(LV_FS_RES_OK != ret) {
                printf("%s create fail!\n", IMGVIEWER_PATH_FILE);
                lv_mem_free(pfile);
                return NULL;
            }
            lv_fs_close(pfile);
            memset(pfile, 0, sizeof(ui_file_t));
            ret = lv_fs_open(pfile, IMGVIEWER_PATH_FILE, LV_FS_MODE_RD | LV_FS_MODE_APPEND);
            if(LV_FS_RES_OK != ret) {
                printf("%s second open failed!\n", IMGVIEWER_PATH_FILE);
                LV_ASSERT_MEM(pfile->file_d);
                lv_mem_free(pfile);
                return NULL;
            }
            img_file_cnt = 0;
            lv_fs_seek(pfile, 0);
            lv_fs_write(pfile, &img_file_cnt, sizeof(uint32_t), &ret_len);
            if(0 == ret_len) {
                printf("write img_file_cnt in sdfs is wrong!\n");
                lv_fs_close(pfile);
                lv_mem_free(pfile);
                return NULL;
            }
            imgviewer_get_img_info(path, pfile);    //get img info
        }

        lv_fs_seek(pfile, 0);
        lv_fs_read(pfile, &img_file_cnt, sizeof(uint32_t), &ret_len);
        if(sizeof(uint32_t) != ret_len) {
            printf("read img_file_cnt in sdfs is wrong!\n");
            lv_fs_close(pfile);
            lv_mem_free(pfile);
            return NULL;
        }
        lv_fs_seek(pfile, index * IMGVIEWER_PATH_MAX * 2 + sizeof(uint32_t));
        memset(&img_info_global, 0, sizeof(img_info_t));  // read data
        lv_fs_read(pfile, img_info_global.path, IMGVIEWER_PATH_MAX, &ret_len);
        if(IMGVIEWER_PATH_MAX != ret_len) {
            printf("read path in sdfs is wrong!\n");
            lv_fs_close(pfile);
            lv_mem_free(pfile);
            return NULL;
        }
        lv_fs_seek(pfile, index * IMGVIEWER_PATH_MAX * 2 + IMGVIEWER_PATH_MAX + sizeof(uint32_t));
        lv_fs_read(pfile, img_info_global.filename, IMGVIEWER_PATH_MAX, &ret_len);
        lv_fs_close(pfile);
        lv_mem_free(pfile);
        if(IMGVIEWER_PATH_MAX != ret_len) {
            printf("read filename in sdfs is wrong!\n");
            return NULL;
        }
    }
    return &img_info_global;
}

static void imgviewer_store_img_info_to_global(char * path, char * filename, ui_file_t * pfile)
{
    //printf("%s: path=%s, filename=%s\n", __FUNCTION__, path, filename);
    char path_temp[IMGVIEWER_PATH_MAX];
    char filename_temp[IMGVIEWER_PATH_MAX];
    uint32_t ret_len = 0;

    if((strlen(filename) + strlen(path) - 1) > IMGVIEWER_PATH_MAX) {
        printf("filename and path are too long, can not be saved!\n");
        return;
    }
    memset(path_temp, 0, IMGVIEWER_PATH_MAX);
    memset(filename_temp, 0, IMGVIEWER_PATH_MAX);
    memcpy(path_temp, path, (strlen(path) - 1)); //without *
    memcpy(filename_temp, filename, strlen(filename));
    img_file_cnt++;
    lv_fs_seek(pfile, 0);
    lv_fs_write(pfile, &img_file_cnt, sizeof(uint32_t), &ret_len);
    if(0 == ret_len) {
        printf("write img_file_cnt in sdfs is wrong!\n");
        return;
    }
    lv_fs_seek(pfile, (img_file_cnt - 1) * IMGVIEWER_PATH_MAX * 2 + sizeof(uint32_t));
    lv_fs_write(pfile, path_temp, IMGVIEWER_PATH_MAX, &ret_len);
    if(0 == ret_len) {
        printf("write path in sdfs is wrong!\n");
        return;
    }
    lv_fs_seek(pfile, (img_file_cnt - 1) * IMGVIEWER_PATH_MAX * 2 + IMGVIEWER_PATH_MAX + sizeof(uint32_t));
    lv_fs_write(pfile, filename_temp, IMGVIEWER_PATH_MAX, &ret_len);
    if(0 == ret_len) {
        printf("write filename in sdfs is wrong!\n");
        return;
    }

    //printf("img_file_cnt:%u\n", img_file_cnt);
}
#endif

#if USE_LV_SD_FS
static void imgviewer_sd_mounted_as_storage(lv_obj_t * obj)
{
    lv_obj_t * label = lv_label_create(obj, NULL);
    if(1 == lv_lang_act()) {
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30);
    } else {
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    }

    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SD_MOUNTED_AS_U_STORAGE);
    lv_obj_align(label, obj, LV_ALIGN_CENTER, 0, 0);

    return;
}
#endif

#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
void imgviewer_for_mount_destory(void)
{
    printf("%s\n", __FUNCTION__);
    imgviewer_destory(true);
}
#endif

#endif /*USE_LV_WATCH_IMGVIEWER*/
